package com.yangcangclub.api.v1.trolley.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangcangclub.commons.annotation.*;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.StringUtil;
import com.yangcangclub.trolley.service.TrolleyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 购物车模块controller
 * Created by 肖明明 on 2017/2/28.
 */
@Controller
@RequestMapping("/yangcangclub/v1/trolley")
public class TrolleyController {

    private static Logger logger = LoggerFactory.getLogger(TrolleyController.class);
   
    @Autowired
    public TrolleyService trolleyService;

    /**添加商品到购物车，带指定数量*/
    @ResponseBody
    @RequestMapping(value = "/add")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData addProductToTrolley(@GetUser User user,
                int pid,int num,
                 HttpServletResponse response){
        ResultData resultData ;
        try {
            //校验
            if(pid<0||num<0)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            //业务
            trolleyService.addToTrolley(pid,user.getId(),num);

            resultData = new ResultData(MessageEnum.SUCCESS);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**获取用户购物车未删除商品列表，pageNum 分页，时间倒排序*/
    @ResponseBody
    @RequestMapping(value = "/list")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData getTrolleyList(@GetUser User user,
             @RequestParam(required = false,defaultValue = "1")int pageNum,
             @RequestParam(required = false,defaultValue = "10")int pageSize,
              HttpServletResponse response){
        ResultData resultData ;
        try {
            //校验
            if(pageNum<1||pageSize<0)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);

            //业务 
            Map m=trolleyService.getTrolleyProductsByPage(user.getId(),pageNum,pageSize);
            resultData = new ResultData(MessageEnum.SUCCESS,m);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
    
    /**根据购物车ID修改购物车内的商品数量，加减一
     * type  类型：1加一，其他减一*/
    @ResponseBody
    @RequestMapping(value = "/update")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData updateTrolleyNum(@GetUser User user,
                int tid,int type,
                HttpServletResponse response){
        ResultData resultData ;
        try {
            //校验
            if(tid<1)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);

            //业务
            boolean result = trolleyService.changeNumOfProductInTrolley(user.getId(),tid,type);
            if(result){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else{
                resultData = new ResultData(MessageEnum.FAILURE);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
    
    /**根据购物车ID列表删除购物车商品列表*/
    @ResponseBody
    @RequestMapping(value = "/del")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData delTrolleyProduct(@GetUser User user,
               String tids,
               HttpServletResponse response){
        ResultData resultData ;
        try {
            //校验
            if(StringUtil.isAllFullEmpty(tids))throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            
            //业务
            String[] ids=tids.split(",");
            if(ids==null||ids.length==0)throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
            List<Integer> ll=Lists.newArrayList();
            for(int i=0;i<ids.length;i++){
                ll.add(Integer.parseInt(ids[i]));
            }
            trolleyService.delProductFromTrolley(user.getId(), ll);

            resultData = new ResultData(MessageEnum.SUCCESS);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }


    
}
